#include "drawwidget.h"

drawwidget::drawwidget(QWidget *parent) : QWidget(parent)
{
    setAutoFillBackground(true);
    setPalette(QPalette(Qt::white));//设置控件背景色
    pix=new QPixmap(size());
    pix->fill(Qt::white);//填充缓冲区背景色
    setMinimumSize(600,400);//设置控件最小尺寸
}

void drawwidget::setWidth(int w)
{
    mywidth=w;//设置线宽
    update();
}

void drawwidget::setStyle(int s)
{
    style=s;//设置线的风格
    update();
}

void drawwidget::mouseMoveEvent(QMouseEvent *event)
{  QPainter painter(pix);//将图绘制在pix上
   QPen pen;
   pen.setStyle(Qt::PenStyle(style));
   pen.setWidth(mywidth);
   pen.setColor(color);
   painter.setPen(pen);
   painter.drawLine(startpos,event->pos());//从鼠标按下到松开的位置画线
   startpos=event->pos();
    update();
}

void drawwidget::paintEvent(QPaintEvent *event)
{  (void)event;
    QPainter painter(this);
    painter.drawPixmap(QPoint(0,0),*pix);

}

void drawwidget::mousePressEvent(QMouseEvent *event)
{
    startpos=event->pos();
    update();
}

void drawwidget::resizeEvent(QResizeEvent *event)
{
    if(width()>pix->width()||height()>pix->height())
    {
        QPixmap* newpix=new QPixmap(size());
        newpix->fill(Qt::white);
        QPainter ps(newpix);//把原本的pix绘在心的pix中
        ps.drawPixmap(QPoint(0,0),*pix);
        pix=newpix;
        update();
    }
  QWidget::resizeEvent(event);
}

void drawwidget::clearFun()
{ QPixmap* cpix=new QPixmap(size());
    cpix->fill(Qt::white);
    pix=cpix;
    update();


}

void drawwidget::setColor(QColor co)
{
    color=co;
    update();
}
